package com.mxprojects.admin
{
	import com.mxprojects.constructors.ButtonLarge;
	import com.mxprojects.constructors.Dropdown;
	import com.mxprojects.constructors.InputConstructor;
	import com.mxprojects.gui.ConfirmDialog;
	import com.mxprojects.gui.DefaultDialog;
	import com.mxprojects.io.Config;
	import com.mxprojects.io.CustomEvent;
	import com.mxprojects.language.Language;
	import com.mxprojects.language.Word;
	import com.mxprojects.views.Admin;
	
	import flash.events.MouseEvent;

	public class AdminGroups extends Admin
	{		
		private static var groupNewName:InputConstructor;
		private static var groupNewDescription:InputConstructor;
		private static var groupCreateButton:ButtonLarge;
		private static var groupDropdown:Dropdown;
		private static var groupEditName:InputConstructor;
		private static var groupEditDescription:InputConstructor;
		private static var groupSaveButton:ButtonLarge;
		private static var groupDeleteButton:ButtonLarge;
		
		private static var leftNextX:int;
		private static var leftNextY:int;
		
		public function AdminGroups(){}
		
		/**
		 *Display the group admin section.
		 * Allows adding, editing and deleting 
		 */		
		public static function display():void {
			//set the text
			setAdminTitle(Language.main(Word.GROUPS));
			
			//setup the content holder
			setupHolders();
			
			//attach some text and a few input fields to the holder
			leftNextX = 0;
			leftNextY = 0;
			displayGroupUI();
			
			//make sure they are at the proper gap
			setLeftAndRight();
		}
		
		/**
		 *Add the UI to handle the group admin 
		 * 
		 */		
		private static function displayGroupUI():void {
			//put the UI elements on the stage
			groupNewName = new InputConstructor(Language.main(Word.NEW_GROUP));
			groupNewName.y = leftNextY;
			leftNextX += groupNewName.inputBox.x;
			leftNextY += groupNewName.height + Config.TEXT_PADDING/2;
			leftHolder.addChild(groupNewName);
			
			groupNewDescription = new InputConstructor(Language.main(Word.DESCRIPTION), "", 2, false, DESCRIPTION_ROWS);
			groupNewDescription.x = Math.round(leftNextX - groupNewDescription.inputLabel.width);
			groupNewDescription.y = leftNextY;
			leftNextY += groupNewDescription.height + Config.TEXT_PADDING/2;
			leftHolder.addChild(groupNewDescription);
			
			groupCreateButton = new ButtonLarge(Language.main(Word.NEW_GROUP), addNewGroup);			
			groupCreateButton.x = leftNextX;
			groupCreateButton.y = leftNextY;
			leftNextY += groupCreateButton.height + Config.TEXT_PADDING*3;
			leftHolder.addChild(groupCreateButton);
			
			//start edit/delete section
			groupDropdown = buildGroupDropdown(groupDropdown, Language.main(Word.SELECT_GROUP));
			groupDropdown.x = leftNextX;
			groupDropdown.y = leftNextY;
			leftNextY += groupDropdown.height + Config.TEXT_PADDING/2;
			groupDropdown.addEventListener(Dropdown.SELECT, groupSelect, false, 0, true);
			leftHolder.addChild(groupDropdown);
			
			groupEditName = new InputConstructor(Language.main(Word.EDIT_GROUP));
			groupEditName.y = leftNextY;
			groupEditName.x = Math.round(leftNextX - groupEditName.inputLabel.width);
			leftNextY += groupEditName.height + Config.TEXT_PADDING/2;
			leftHolder.addChild(groupEditName);
			
			groupEditDescription = new InputConstructor(Language.main(Word.DESCRIPTION), "", 2, false, DESCRIPTION_ROWS);
			groupEditDescription.x = Math.round(leftNextX - groupEditDescription.inputLabel.width);
			groupEditDescription.y = leftNextY;
			leftNextY += groupEditDescription.height + Config.TEXT_PADDING/2;
			leftHolder.addChild(groupEditDescription);
			
			groupSaveButton = new ButtonLarge(Language.main(Word.SAVE_GROUP), saveGroup);
			groupSaveButton.x = leftNextX;
			groupSaveButton.y = leftNextY;
			leftHolder.addChild(groupSaveButton);
			
			groupDeleteButton = new ButtonLarge("<span class=\"buttonAdminText\">" + Language.main(Word.DELETE_GROUP) + "</span>", deleteGroup);
			groupDeleteButton.x = leftNextX + groupSaveButton.width + Config.TEXT_PADDING;
			groupDeleteButton.y = leftNextY;
			leftNextY += groupDeleteButton.height + Config.TEXT_PADDING/2;
			leftHolder.addChild(groupDeleteButton);
		}
		
		/**
		 * Create a new group
		 * @param event
		 * 
		 */		
		private static function addNewGroup(event:MouseEvent):void {
			//make sure that they have the field filled out
			if(groupNewName.getValue() == "" || groupNewDescription.getValue() == ""){
				DefaultDialog.show(Language.main(Word.ERROR_REQURED_FIELDS));
				return;
			}
			
			//prepare the data to head to the server
			dataToSend = new Object();
			dataToSend.action = AdminActions.ADD_GROUP;
			dataToSend.groupName = groupNewName.getValue();
			dataToSend.groupDescription = groupNewDescription.getValue();
			
			var adminHandleData:AdminHandleData = new AdminHandleData(dataToSend, newGroupSuccess, groupCreateButton, ADMIN_SCRIPT);
		}
		
		/**
		 *Returned from the server after we've created a new group 
		 * @param data
		 * 
		 */		
		private static function newGroupSuccess(data:Object):void {
			//new group created
			DefaultDialog.show(Language.main(Word.MESSAGE_NEW_GROUP), true, Word.SUCCESS);
			
			//clean things up and add the new group to the dropdown
			dataFromServer = data;
			groupNewName.inputBox.text = "";
			groupNewDescription.inputBox.text = "";
			
			groupDropdown = buildGroupDropdown(groupDropdown, Language.main(Word.SELECT_GROUP));
		}
		
		/**
		 * Delete the currently selected group 
		 * @param event
		 * 
		 */		
		private static function deleteGroup(event:MouseEvent):void {
			//anything to delete?
			if(groupDropdown.value == Dropdown.NULL_VALUE){
				return;
			}
			
			//make sure this is something they want to do
			ConfirmDialog.confirm(Language.main(Word.DELETE_CONFIRM), Language.main(Word.DELETE, true), deleteGroupConfirm);
		}
		
		/**
		 * Tell the server that we want to delete the group 
		 * 
		 */		
		private static function deleteGroupConfirm():void {
			//send the data to the server
			dataToSend = new Object();
			dataToSend.action = AdminActions.DELETE_GROUP;
			dataToSend.groupID = groupDropdown.value;
			
			var adminHandleData:AdminHandleData = new AdminHandleData(dataToSend, deleteGroupSuccess, groupDeleteButton,  ADMIN_SCRIPT);
		}
		
		/**
		 * Response from the server after the group has been deleted 
		 * @param data
		 * 
		 */		
		private static function deleteGroupSuccess(data:Object):void {
			DefaultDialog.show(Language.main(Word.MESSAGE_DELETE_GROUP), true, Word.SUCCESS);
			
			//all went well, so refresh the dropown and clean out the inputs
			dataFromServer = data;
			
			groupEditName.inputBox.text = "";
			groupEditDescription.inputBox.text = "";
			
			groupDropdown = buildGroupDropdown(groupDropdown, Language.main(Word.SELECT_GROUP));
		}
		
		/**
		 * Takes the currently selected group and updates the name and the description 
		 * @param event
		 */		
		private static function saveGroup(event:MouseEvent):void {
			//make sure that we have data to send
			if(groupEditName.getValue() == "" || groupEditDescription.getValue() == ""){
				DefaultDialog.show(Language.main(Word.ERROR_REQURED_FIELDS));
				return;
			}
			
			//prepare the data for the server
			dataToSend = new Object();
			dataToSend.action = AdminActions.SAVE_GROUP;
			dataToSend.groupID = groupDropdown.value;
			dataToSend.groupName = groupEditName.getValue();
			dataToSend.groupDescription = groupEditDescription.getValue();
			
			var adminHandleData:AdminHandleData = new AdminHandleData(dataToSend, saveGroupSuccess, groupSaveButton,  ADMIN_SCRIPT);
		}
		
		/**
		 * Success result back from the server once we've saved the values 
		 * @param data
		 * 
		 */		
		private static function saveGroupSuccess(data:Object):void {
			DefaultDialog.show(Language.main(Word.MESSAGE_SAVE_GROUP), true, Word.SUCCESS);
			
			//all went well, so refresh the dropown and clean out the inputs
			dataFromServer = data;
			
			groupEditName.inputBox.text = "";
			groupEditDescription.inputBox.text = "";
			
			groupDropdown = buildGroupDropdown(groupDropdown, Language.main(Word.SELECT_GROUP));
		}
		
		/**
		 * When the group dropdown is changed this will make sure that the delete button
		 * only shows up if they have selected a custom group
		 * @param event
		 * 
		 */
		private static function groupSelect(event:CustomEvent):void {
			var groupID:int = event.customParameter[1];
			
			groupDeleteButton.enabled = true;
			
			if(groupID <= 4){
				groupDeleteButton.enabled = false;
			}
			
			//populate the edit name and description fields
			for(var i:int = 0; i < dataFromServer.groupTotal; i++){
				if(int(dataFromServer["group" + i + "ID"]) == groupID){
					//toss them in the inputs
					groupEditName.inputBox.text = dataFromServer["group" + i + "Name"];
					groupEditDescription.inputBox.text = dataFromServer["group" + i + "Description"];					
					break;
				}
			}
		}
		
	}
}